package com.ubb.damate.webservice;

import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.ubb.damate.model.EventTypes;
import com.ubb.damate.model.Project;
import com.ubb.damate.model.ProjectPK;
import com.ubb.damate.model.UserEvent;
import com.ubb.damate.model.UserEventPK;
import com.ubb.damate.model.Workspace;
import com.ubb.damate.service.ProjectServiceLocal;
import com.ubb.damate.service.UserEventServiceLocal;
import com.ubb.damate.service.WorkspaceServiceLocal;

/** Session Bean implementation class UserEventWebService */
@Stateless
@WebService()
public class UserEventWebService {
	@EJB(beanName = "UserEventService")
	private UserEventServiceLocal	userEventService;
	@EJB(beanName = "WorkspaceService")
	private WorkspaceServiceLocal	workspaceService;
	@EJB(beanName = "ProjectService")
	private ProjectServiceLocal		projectService;

	@WebMethod(action = "createOrUpdateUserEvent", operationName = "createOrUpdateUserEvent")
	public boolean createOrUpdateUserEvent(@WebParam(name = "eventvalue") String eventvalue, @WebParam(name = "username") String username, @WebParam(name = "eventname") String eventname,
			@WebParam(name = "date") Date date) {

		Workspace workspace = null;
		Project project = null;
		switch (EventTypes.valueOf(eventname)) {
			case CreateWorkspace:
				workspace = new Workspace();
				workspace.setWorkspaceId(0);
				workspace.setCreationDate(date);
				workspace.setUsername(username);
				workspace.setPath(eventvalue);

				workspaceService.createOrUpdate(workspace);
				break;
			case CreateProject:
				String[] tmp = eventvalue.split(";");

				workspace = workspaceService.find(username, tmp[0]);

				ProjectPK projectPK = new ProjectPK();
				projectPK.setProjectName(tmp[1]);
				projectPK.setWorkspaceId(workspace.getWorkspaceId());

				project = new Project();
				project.setCreationDate(new Date());
				project.setId(projectPK);

				projectService.createOrUpdate(project);
				break;
			case RunFail:
			case RunSuccess:
			case OpenTime:
			default:
				UserEventPK userPk = new UserEventPK();
				userPk.setEventname(eventname);
				userPk.setUsername(username);
				userPk.setEventdate(date);

				UserEvent userEvent = new UserEvent();
				userEvent.setId(userPk);
				userEvent.setEventvalue(eventvalue);

				userEventService.createOrUpdate(userEvent);
				break;
		}

		return true;
	}

	/*public boolean createOrUpdateUserEvent(@WebParam(name = "eventvalue") DAMATEEventValue eventvalue, @WebParam(name = "username") String username, @WebParam(name = "eventname") String eventname,
			@WebParam(name = "date") Date date) {
		Workspace workspace = null;
		Project project = null;
		switch (EventTypes.valueOf(eventname)) {
			case CreateProject:
				workspace = new Workspace();
				workspace.setWorkspaceId(0);
				workspace.setCreationDate(date);
				workspace.setUsername(username);
				workspace.setPath(eventvalue.getWorkspace().getWorkspacePath() + eventvalue.getWorkspace().getWorkspaceName());

				workspaceService.createOrUpdate(workspace);
				break;
			case CreateWorkspace:
				workspace = workspaceService.find(username, eventvalue.getWorkspace().getWorkspacePath() + eventvalue.getWorkspace().getWorkspaceName());

				ProjectPK projectPK = new ProjectPK();
				projectPK.setProjectName(eventvalue.getProject().getProjectName());
				projectPK.setWorkspaceId(workspace.getWorkspaceId());

				project = new Project();
				project.setCreationDate(new Date());
				project.setId(projectPK);

				projectService.createOrUpdate(project);
				break;
			case RunFail:
			case RunSuccess:
			case OpenTime:
			default:
				UserEventPK userPk = new UserEventPK();
				userPk.setEventname(eventname);
				userPk.setUsername(username);
				userPk.setEventdate(date);

				UserEvent userEvent = new UserEvent();
				userEvent.setId(userPk);
				userEvent.setEventvalue("" + eventvalue.getTimeIntervalInMinutes());

				userEventService.createOrUpdate(userEvent);
				break;
		}

		return true;
	}*/
}
